package com.gxa.controller;

import com.gxa.common.R;
import com.gxa.pojo.*;
import com.gxa.pojo.blackPojo.ExcelWrite;
import com.gxa.pojo.blackPojo.ShowBack;
import com.gxa.service.EmpEntryService;
import com.gxa.service.EmpExcelWriteService;
import com.gxa.service.MonthGoalService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/emp_upload")
@Api(value = "录入员工回显" ,tags = "录入员工回显接口")
public class ShowBackController {


    @Autowired
    private EmpEntryService empEntryService;

    //上传文件
    @RequestMapping(value = "/uploadfile",method = RequestMethod.POST)
    @ApiOperation(value = "员工录入",notes = "",httpMethod = "POST", response = Emp.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ResponseBody
    public R upLoadFile(EmpEntry fileUser, HttpServletRequest request) throws IOException {
            R r = new R();
////            System.out.println(fileUser.getUsername());
////            System.out.println(fileUser.getPassword());
//            if(fileUser.getSelfPic()==null||fileUser.getSelfPic().isEmpty()){
//                r.setCode(500);
//                r.setMsg("没有选中文件");
//            } else{//正常上传
//
//                String uploadPath =  File.separator + "upload" + File.separator;
//                File file = new File(uploadPath);
//                if(!file.exists()){
//                    file.mkdirs();
//                }
//                MultipartFile multipartFile = fileUser.getSelfPic();
//                String fileName = multipartFile.getOriginalFilename();
//                File newFile = new File(uploadPath + fileName);
//                System.out.println("哈哈newFile=" + newFile.toString());
//                //上传
//                FileCopyUtils.copy(multipartFile.getBytes(),newFile);
//                fileUser.setEmp_pic(newFile.toString());
//                r.setCode(200);
//                r.setMsg("成功");
//            }
            if(fileUser.getEntryPic()==null||fileUser.getEntryPic().isEmpty()){
                r.setCode(500);
                r.setMsg("没有选中文件");
            } else{//正常上传
                String uploadPath = File.separator + "upload" + File.separator;
                File file = new File(uploadPath);
                if(!file.exists()){
                    file.mkdirs();
                }
                MultipartFile multipartFile = fileUser.getEntryPic();
                String fileName = multipartFile.getOriginalFilename();
                File newFile = new File(uploadPath + fileName);
                System.out.println("newFile=" + newFile);
                //上传
                FileCopyUtils.copy(multipartFile.getBytes(),newFile);
                fileUser.setEmp_joinTb(newFile.toString());
                r.setCode(200);
                r.setMsg("成功");
            }
            if(fileUser.getIdPic()==null||fileUser.getIdPic().isEmpty()){
                r.setCode(500);
                r.setMsg("没有选中文件");
            } else{//正常上传

                String uploadPath =File.separator + "upload" + File.separator;
                File file = new File(uploadPath);
                if(!file.exists()){
                    file.mkdirs();
                }
                MultipartFile multipartFile = fileUser.getIdPic();
                String fileName = multipartFile.getOriginalFilename();
                File newFile = new File(uploadPath + fileName);
                System.out.println("newFile=" + newFile);
                //上传
                FileCopyUtils.copy(multipartFile.getBytes(),newFile);
                fileUser.setEmp_idCardAddress(newFile.toString());
                r.setCode(200);
                r.setMsg("成功");
            }
           int result= empEntryService.empEntry(fileUser);
            if (result>-1){
                r.setCode(200);
                r.setMsg("success");
            }
            //调用Service
            return r;
        }


}
